package com.byxy.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import com.byxy.dao.OrderDao;
import com.byxy.entity.Order;
import com.byxy.service.OrderService;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
	@Resource
	private OrderDao orderDao;

	@Override
	public void addEXCL(InputStream fileInputStream) {
		try {
			HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
			HSSFSheet sheet = workbook.getSheetAt(0);
			for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
				HSSFRow row = sheet.getRow(i);
				HSSFCell cell = row.getCell(0);
				String string = cell.getStringCellValue();
				cell = row.getCell(1);
				String string2 = cell.getStringCellValue();
				cell = row.getCell(2);
				double d1 = cell.getNumericCellValue();
				cell = row.getCell(3);
				double d2 = cell.getNumericCellValue();
				cell = row.getCell(4);
				double d3 = cell.getNumericCellValue();

				if (string.equals(""))
					break;
				orderDao.add(new Order(string, string2, (int) d1, (int) d2, (int) d3));
			}
			workbook.close();
			fileInputStream.close();
		} catch (

		IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<Order> find() {
		return orderDao.find();
	}
}
